package yunjiao.javatutorials.guava.base.pairwise;

import com.google.common.base.Equivalence;

import java.util.Arrays;
import java.util.List;

/**
 *  与 Java 8 Stream API 结合
 *
 * @author yangyunjiao
 */
public class StreamIntegration {
    public static void main(String[] args) {
        Equivalence<String> vowelCountEquivalence = new Equivalence<String>() {
            private int countVowels(String s) {
                if (s == null) return 0;
                int count = (int) s.toLowerCase().chars()
                        .filter(c -> "aeiou".indexOf(c) >= 0)
                        .count();
                System.out.printf("%s = %d %n", s, count);
                return count;
            }

            @Override
            protected boolean doEquivalent(String a, String b) {
                return countVowels(a) == countVowels(b);
            }

            @Override
            protected int doHash(String s) {
                return countVowels(s);
            }
        };

        Equivalence<Iterable<String>> pairwiseByVowels = vowelCountEquivalence.pairwise();

        List<String> words1 = Arrays.asList("apple", "elephant", "igloo");
        List<String> words2 = Arrays.asList("orange", "echo", "umbrella");

        // 短路判断
        System.out.println("元音数量结构相同: " +
                pairwiseByVowels.equivalent(words1, words2)); // false
    }
}
